﻿package kit4j.app.notepad;

import java.awt.AWTException;
import java.awt.SystemTray;
import java.awt.TrayIcon;

import kit4j.app.notepad.command.ExitCommand;
import kit4j.app.notepad.factory.NotepadFactory;
import kit4j.app.notepad.module.NotepadModule;
import kit4j.app.notepad.service.ExceptionCatchService;
import kit4j.app.notepad.ui.comp.NotepadTrayIcon;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Guice;
import com.google.inject.Injector;

/**
 * 程序运行入口。
 * 
 * @author MiniKnife
 * 
 */
public class NotepadApp {

	private static final Logger logger = LoggerFactory.getLogger(NotepadApp.class);

	public static void main(String[] args) throws Exception {
		logger.info("Start up.");

		Injector injector = Guice.createInjector(new NotepadModule());
		NotepadFactory notepadFactory = injector.getInstance(NotepadFactory.class);
		NotepadFactory.setNotepadFactoryInstance(notepadFactory);
		
		try {
			installTrayIcon(injector.getInstance(NotepadTrayIcon.class));
		} catch (AWTException e) {
			ExceptionCatchService.getInstance().catchException(e);
			injector.getInstance(ExitCommand.class).execute();
			
			return;
		}
		
		NotepadFactory.getInstance().showNewNotepad();
	}

	private static void installTrayIcon(TrayIcon trayIcon) throws AWTException {
		if (SystemTray.isSupported()) {
			SystemTray.getSystemTray().add(trayIcon);
		} else {
			logger.error("System not support tray icon!");
		}
	}
}
